<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-24
 * Time: 10:49
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
use qcos\Cos;


class AdvertController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $a_type = isset($param['a_type']) ? $param['a_type'] : 0;
        $a_status = isset($param['a_status']) ? $param['a_status'] : 0;
        $where = null;
        if($a_type){ $where['a_type'] = $a_type;}else{ unset($param['a_type']);}
        if($a_status){ $where['a_status'] = $a_status;}else{ unset($param['a_status']);}
        $advertising = DB::name('advertising')
            ->where($where)
            ->order(['id' => 'DESC'])
            ->paginate(20);
        $advertising->appends($param);
        $page = $advertising->render();
        $this->assign("a_type", $a_type);
        $this->assign("a_status", $a_status);
        $this->assign("advertising", $advertising);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function stars()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where = null;
        if($s_name){ $where['s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $star = Db::name('star_list')->where($where)->order(['id' => 'DESC'])->paginate(5);
        $star->appends($param);
        $page = $star->render();
        $this->assign("s_name", $s_name);
        $this->assign("star", $star);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        $this->assign("version", VERSION_LIVESTAR);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'a_digital' => 'require|alphaNum',
                'a_title' => 'require|chsAlphaNum|max:16',
                'a_udid' => 'number',
                'a_sort' => 'require|number|between:1,100',
                'a_url' => 'min:16|max:128',
                'a_status' => 'require|number|between:0,1000',
                'a_type' => 'require|number|between:0,100',
                'path' => 'require',
                'file' => 'require',
            ]);
            $validate->message([
                'a_digital.require' => '版本号不能为空',
                'a_digital.alphaNum' => '版本号只能为字母和数字',
                'a_title.require' => '广告标题不能为空',
                'a_title.alphaNum' => '广告标题只能是汉字、字母和数字',
                'a_title.max' => '广告标题最大长度为16个字符',
                'a_udid.number' => '请选择明星',
                'a_sort.require' => '排序编号不可为空',
                'a_sort.number' => '排序编号只能是数字',
                'a_sort.between' => '排序编号只能在1~100之间',
                'a_url.min' => '跳转链接最小长度为16个字符',
                'a_url.max' => '跳转链接最大长度为128个字符',
                'a_type.require' => '请选择广告类型',
                'a_type.number' => '请选择广告类型',
                'a_status.number' => '请选择状态',
                'a_status.require' => '请选择状态',
                'path.require' => '广告图不可为空',
                'file.require' => '广告图不可为空',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $path = $data['path'];
                $file = $data['file'];
                if($path && $file){
                    $data['a_image'] = $this->moveImg($path,$file,6);
                }
                unset($data['file']);
                unset($data['path']);
                $data['a_userid'] = cmf_get_current_admin_id();
                Db::name('advertising')->insert($data);
                $this->success('添加成功',url('advert/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $advertising = DB::name('advertising')->where(["id" => $id])->find();
        if($advertising['a_type'] == 20 && $advertising['a_udid'] != 0){
            $star = DB::name('star_list')->where(["s_udid" => $advertising['a_udid']])->find();
            $star = $star['s_name'];
        }else{
            $star = '全部明星';
        }
        $this->assign("star", $star);
        $this->assign("advertising", $advertising);
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        $this->assign("version", VERSION_LIVESTAR);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'a_digital' => 'require|alphaNum',
                'a_title' => 'require|chsAlphaNum|max:16',
                'a_sort' => 'require|number|between:1,100',
                'a_url' => 'min:16|max:128',
                'a_status' => 'require|number|between:0,1000',
            ]);
            $validate->message([
                'a_digital.require' => '版本号不能为空',
                'a_digital.alphaNum' => '版本号只能为字母和数字',
                'a_title.require' => '广告标题不能为空',
                'a_title.alphaNum' => '广告标题只能是汉字、字母和数字',
                'a_title.max' => '广告标题最大长度为16个字符',
                'a_sort.require' => '排序编号不可为空',
                'a_sort.number' => '排序编号只能是数字',
                'a_sort.between' => '排序编号只能在1~100之间',
                'a_url.min' => '跳转链接最小长度为16个字符',
                'a_url.max' => '跳转链接最大长度为128个字符',
                'a_status.number' => '请选择状态',
                'a_status.require' => '请选择状态',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $path = $data['path'];
                $file = $data['file'];
                if($path && $file){
                    $imgUrl = parse_url($data['a_image']);
                    $hostUrl = parse_url(HTTP_IMG);
                    if(!empty($imgUrl['host']) && !empty($hostUrl['host']) && ($imgUrl['host'] == $hostUrl['host'])){
                        $this->deleteImg($imgUrl['path']);
                    }
                    $data['a_image'] = $this->moveImg($path,$file,6);
                }
                unset($data['path']);
                unset($data['file']);
                Db::name('advertising')->update($data);

                $this->success('修改成功',url('advert/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function enabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('advertising')->where(["id" => $id])->find();
        switch ($shout['a_status']){
            case 10:
                DB::name('advertising')->where(["id" => $id])->setField('a_status', 20);
                $this->success('您已停用此广告');
                break;
            case 20:
                DB::name('advertising')->where(["id" => $id])->setField('a_status', 10);
                $this->success('您已启用此广告');
                break;
            default:
                DB::name('advertising')->where(["id" => $id])->setField('a_status', 20);
                $this->error('未知的广告状态,已置为停用');
        }
    }

    public function order()
    {
        $model = DB::name('advertising');
        if (!is_object($model)) {
            return false;
        }

        $pk  = $model->getPk(); //获取主键名称
        $ids = $this->request->post("a_sort/a");

        if (!empty($ids)) {
            foreach ($ids as $key => $r) {
                $data['a_sort'] = $r;
                $model->where([$pk => $key])->update($data);
            }

        }
        $this->success("排序更新成功！");
    }

    public function uploadImg()
    {
        $file = $_FILES['file'];
        $root = VERSION_LIVESTAR.'/Default/';
        $pathName['path'] = date('Y/m/');
        $pathName['name'] = microtime (true)*10000;
        $path = $root.$pathName['path'];
        $cos = new Cos();
        $type = pathinfo($file["name"]);
        $info = $cos->upload($file["tmp_name"],$path.$pathName['name'].'.'.$type['extension']);
        if($info){
            $data['code'] = 1;
            $data['msg'] = '上传成功';
            $data['data'] = ['path' => $pathName['path'],'file' => $pathName['name'].'.'.$type['extension']];
            $data['url'] = '';
        }else{
            $data['code'] = 0;
            $data['msg'] = '上传错误';
            $data['data'] = [];
            $data['url'] = '';
        }
        return json_encode($data);
    }
}